<?php
	class ArticleAction extends HomeAction{
	
		//文章内容
		public function show(){
	        	
	        	$id=$this->_get('id');	//内容页ID

		        //内容主体
		        $key=md5('content'.$id);
		        $key=F($key);

		        if(!empty($key)){
		        		$this->header();    	//导入页头
		        		$res=F($key);
						$res['content']=stripslashes($res['content']);
						$res['addtime']=date('Y-m-d',$res['addtime']);
						$this->assign('keywords',$res['keywords']);
						$this->assign('description',$res['description']);
						$this->assign('seotitle',$res['seotitle']);	
						$this->assign('list',$res);			//内容输出
						
						$hits=self::saveHits($res['typeid']);		//计算点击率
						$this->assign('hits',$hits);

						$this->display();
		        	}else{
		        		$article=D('Article');
						$res=$article->relation(true)->find($id);
						if($res){
							$this->header();    	//导入页头
							F($key,$res);	

							$res['content']=stripslashes($res['content']);

							$res['addtime']=date('Y-m-d',$res['addtime']);

							$this->assign('keywords',$res['keywords']);
							$this->assign('description',$res['description']);
							$this->assign('seotitle',$res['seotitle']);
							
							$this->assign('list',$res);			//内容输出
							
							$hits=self::saveHits($res['typeid']);		//计算点击率
							$this->assign('hits',$hits);

							$this->display();
						}else{
							self::_empty();
						}
				
					}

		}



		//发布评论
		Public function comment(){

			$data['comm_content_id']=$_POST['content_id'];		//内容ID
			$data['comm_username']=$_POST['username'];		//用户名
			$data['comm_username_id']=$_POST['username_id'];	//用户ID
			$data['comm_content']=nl2br($_POST['Content']);		//评论内容
			$data['comm_email']=$_POST['email'];			//评论者EMAIL
			$data['pid']=$_POST['pid'];					//评论者EMAIL
			$data['Verify']=$_POST['Verify'];				//验证码
			$data['ip']=get_client_ip();					//IP地址
			$data['status']=2;						//状态为未审核
			$data['addtime']=time();					//评论时间
			$data['success']=1;						//用于发布成功返回提示

			//数据安全过滤
			$data['comm_content']=strip_tags($data['comm_content'],'<p><br/><br>');
			$data['comm_username']=strip_tags($data['comm_username']);
			$data['comm_email']=strip_tags($data['comm_email']);

			//验证码判断
			if($_SESSION['verify']!=md5($data['Verify'])){
				$this->ajaxReturn('验证码不正确');
			}
			
			$comment=D('Comment');

			if (!$comment->create($data)){
				$this->ajaxReturn($comment->getError());//输出错误 
			}else{

			  	$res=$comment->filter('strip_tags')->add($data);

			  	if($res){
			  		$data['addtime']=date('Y-m-d H:i:s',$data['addtime']);
			  		if(isset($data['pid'])){
			  			$list=$comment->field('comm_username,comm_content')->find($data['pid']);
			  			$data['pid_username']=$list['comm_username'];
			  			$data['pid_content']=$list['comm_content'];
			  		}
			  		$this->ajaxReturn($data);
			  	}else{
			  		$this->ajaxReturn('发布失败！');
			  	}

			}

		}

		//加载评论列表
		public function commentList(){
			$comment=M('Comment');
			$sql=array('content_id'=>$_GET['id']);
			$list=$comment->where()->select();
			$this->assign('list',$list);
		}




 		//更新点击率
		static protected function saveHits($typeid){

		        	$hits=M('hits');

		        	$time=strtotime(date('Ymd',time()));	//获得当前整点日期的时间戳

		        	$data['date']=$time;					//时间
		        	$data['typeid']=$typeid;				//栏目id
		        	$data['content_id']=$_GET['id'];		//内容id

		        	//查询条件
		        	$sql['where']['content_id']=$data['content_id'];
		        	$sql['where']['date']=$time;

		        	$hitsRes=$hits->where($sql['where'])->find();

		        	if(empty($hitsRes)){
		        		$data['hits']=1;
		        		$hits->add($data);
		        		return $hits->where('content_id='.$data['content_id'])->Sum('hits');
		        	}else{
		        		$data['hits']=$hitsRes['hits']+1;
		        		$hits->where($sql['where'])->save($data);
		        		return $hits->where('content_id='.$data['content_id'])->Sum('hits');
		        	}

		}

		/**获得单条内容伪静态URL
		*	参数：由find获得的单条内容数组
		*	返回：在原数组中增加URL键
		**/
		static protected function getContentUrl($arr){
			$menu=M('Menu');
			$res=$menu->where('typeid='.$arr['typeid'])->field('contenturl')->find();
			$arr['url']=U($res['contenturl'].$arr['id']);
			return $arr;
		}

	    Public function Verify(){
	        import('ORG.Util.Image');
	        Image::buildImageVerify();
	    }

	}
